package offer.algorithm;


import java.util.Stack;

/**
 * Description：
 * Author: zhangc
 * Date：2017/7/20 15:19
 */
public class MinStack21<T extends Comparable<? super T>> {
    private Stack<T> mainStack = new Stack<>();
    private Stack<T> assistStack = new Stack<>();

    public void push(T val) {
        if (mainStack.isEmpty()) {
            assistStack.push(val);
        } else {
            if (assistStack.peek().compareTo(val) <= 0)
                assistStack.push(assistStack.peek());
            else
                assistStack.push(val);
        }
        mainStack.push(val);
    }

    public T pop() {
        if (mainStack.isEmpty())
            return null;
        assistStack.pop();
        return mainStack.pop();
    }

    public boolean isEmpty() {
        return mainStack.isEmpty();
    }

    public T getMin() {
        return mainStack.isEmpty() ? null : assistStack.peek();
    }
}
